/*
 *
 * author:dustin
 * email:dushaobindoudou@gmail.com
 *
 * 滚动条滚动到底部时加载更多列表项
 * 这个也可以认为是延迟加载
 *
 */
(function(win){
    var doc = win.document;
    //获取窗口大小
    var winSize = function(){
        var b, d;
        var e = doc;
        if (e.compatMode === "CSS1Compat") {
            b = e.documentElement.clientWidth;
            d = e.documentElement.clientHeight
        }
        else {
            if (win.innerHeight) {
                e = win;
                b = e.innerWidth;
                d = e.innerHeight
            }
            else {
                if (e.documentElement && e.documentElement.clientHeight) {
                    b = e.documentElement.clientWidth;
                    d = e.documentElement.clientHeight
                }
                else {
                    if (e.body) {
                        b = e.body.clientWidth;
                        d = e.body.clientHeight
                    }
                }
            }
        }
        return {
            width: b,
            height: d
        }
    };
    //获取元素的offset实际位置
    var findAbsPos = function(el){
        var curLeft = curTop = 0;
        if (el.getBoundingClientRect) { // IE
            box = el.getBoundingClientRect();
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
            var docElem = document.documentElement;
            var clientTop = docElem.clientTop || document.body.clientTop || 0;
            var clientLeft = docElem.clientLeft || document.body.clientLeft || 0;
            curLeft = box.left + scrollLeft - clientLeft;
            curTop = box.top + scrollTop - clientTop;
        }
        else {
            if (el.offsetParent) {
                do {
                    curLeft += el.offsetLeft;
                    curTop += el.offsetTop;
                }
                while (el = el.offsetParent);
            }
        }
        return {
            left: curLeft,
            top: curTop
        }
    }
    //获取元素相对页面的位置
    var findPos = function(obj){
        var thePos = findAbsPos(obj);
        var parent;
        if (obj.parentNode) {
            parent = obj.parentNode;
        }
        else {
            parent = null;
        }
        while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') {
            thePos.left -= parent.scrollLeft;
            thePos.top -= parent.scrollTop;            
            if (parent.parentNode) {
                parent = parent.parentNode;
            }
            else {
                parent = null;
            }
        }
        return {
            left: thePos.left,
            top: thePos.top
        };
    };
    //获取页面滚动条滚动多少
    var getPageScroll = function(){
        var xScroll, yScroll;
        if (self.pageYOffset) {
            yScroll = self.pageYOffset;
            xScroll = self.pageXOffset;
        }
        else 
            if (document.documentElement && document.documentElement.scrollTop) {
                yScroll = document.documentElement.scrollTop;
                xScroll = document.documentElement.scrollLeft;
            }
            else 
                if (document.body) {// all other Explorers
                    yScroll = document.body.scrollTop;
                    xScroll = document.body.scrollLeft;
                }
        return {
            scrollLeft: xScroll,
            scrollTop: yScroll
        }
    };
    //得到元素相对窗口的位置
    //todo:ie可以用getBoundingClientRect需要处理clientLeft，clientTop
    var findPosRelativeToViewport = function(obj){
        var objPos = findPos(obj)
        var scroll = getPageScroll()
        return {
            left: objPos.left - scroll.scrollLeft,
            top: objPos.top - scroll.scrollTop
        }
    }
    //获得页面高度
    var pageSize = function(){
        var target = doc;
        var h = (target.compatMode == "CSS1Compat" ? target.documentElement : target.body);
        var g, c;
        var f, e;
        if (window.innerHeight && window.scrollMaxY) {
            g = h.scrollWidth;
            c = window.innerHeight + window.scrollMaxY
        }
        else {
            if (h.scrollHeight > h.offsetHeight) {
                g = h.scrollWidth;
                c = h.scrollHeight
            }
            else {
                g = h.offsetWidth;
                c = h.offsetHeight
            }
        }
        var b = winSize();
        if (c < b.height) {
            f = b.height
        }
        else {
            f = c
        }
        if (g < b.width) {
            e = b.width
        }
        else {
            e = g
        }
        return {
            page: {
                width: e,
                height: f
            },
            win: {
                width: b.width,
                height: b.height
            }
        }
    }
    //检测事件支持
    var addEvent = function(ele, evt, fun){
        if (ele.addEventListener) {
            ele.addEventListener(evt, fun, false);
        }
        else 
            if (ele.attachEvent) {
                ele.attachEvent("on" + evt, fun);
            }
    }
    var stopEvent = function(e){
        if (!e) 
            var e = window.event;
        
        //e.cancelBubble is supported by IE - this will kill the bubbling process.
        e.cancelBubble = true;
        e.returnValue = false;
        
        //e.stopPropagation works only in Firefox.
        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        }
        return false;
    }
    
    var LoadMore = function(ele, callback, pannel){
        this.callback = callback;
        this.ele = ele || "body";
        this.pannel = pannel || win;
        this.running = false;
        
        this.init();
    }
    LoadMore.prototype = {
        constructor: LoadMore,
        init: function(){
            var that = this;
            //添加scroll事件
            addEvent(that.pannel, "scroll", function(){
                that.checkCurrentState();
                
            });
            //添加resize事件
            addEvent(that.pannel, "resize", function(){
                that.checkCurrentState();                
            });
			//初次检测
			this.checkCurrentState();
        },
        checkCurrentState: function(){
            var that = this;
            var pos = findPos(that.ele);
            var scrollPos = getPageScroll();
            var ws = winSize();
            if (scrollPos.scrollTop + ws.height >= pos.top && scrollPos.scrollLeft + ws.width >= pos.left) {
                if (!that.running) {
                    that.running = true;
                    that.callback.call(that);
                }
            }
        }
    }
    
    win.LoadMore = LoadMore;
    
})(window)
